import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

//模拟实现线程池
class MyPool {
    //核心数据结构
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    //安排任务
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    //构造方法
    public MyPool(int n) {
        for (int i = 0;i < n;i++) {
            Thread thread = new Thread(() -> {
                try {
                    while (true) {
                        Runnable runnable = queue.take();
                        runnable.run();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
    }

}

public class ThreadDemo18 {
    public static void main(String[] args) throws InterruptedException {
        //ExecutorService pool = Executors.newFixedThreadPool(10);
        MyPool pool = new MyPool(10);
        for (int i = 0; i < 1000; i++) {
            int num = i;
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello" + num);
                }
            });
        }
    }
}
